package com.koron.sinoprobe.web.engineering.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.common.CommonUtils;
import com.koron.sinoprobe.web.engineering.bean.dto.SpDivisionDTO;
import com.koron.sinoprobe.web.engineering.bean.po.SpDivision;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.query.SpDivisionQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpDivisionProjectVO;
import com.koron.sinoprobe.web.engineering.bean.vo.SpDivisionVO;
import com.koron.sinoprobe.web.engineering.mapper.SpDivisionMapper;
import com.koron.sinoprobe.web.engineering.service.SpDivisionProjectService;
import com.koron.sinoprobe.web.engineering.service.SpDivisionService;
import com.koron.sinoprobe.web.engineering.service.SpEstablishService;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 项目划分(SpDivision)表服务实现类
 *
 * @author jzp
 * @since 2021-12-01 08:34:14
 */
@Service
public class SpDivisionServiceImpl implements SpDivisionService {

    @Autowired
    private SpDivisionProjectService spDivisionProjectService;

    @Autowired
    private SpEstablishService spEstablishService;

    @Autowired
    private SpProjectService spProjectService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpDivisionVO getById(SessionFactory factory, String id) {
        SpDivisionMapper mapper = factory.getMapper(SpDivisionMapper.class);
        SpDivisionVO spDivisionVO = mapper.selectById(id);
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spDivisionVO.getProjectId());
        Assert.notNull(spEstablish,"该项目【" + spDivisionVO.getProjectName() + "】未立项，请先进行项目立项！");
        spDivisionVO.setProjectCategory(spEstablish.getProjectCategory());
        spDivisionVO.setMajorCategory(spEstablish.getMajorCategory());
        spDivisionVO.setSpEstablish(spEstablish);
        SpProject spProject = spProjectService.getById(factory, spDivisionVO.getProjectId());
        spDivisionVO.setSpProject(spProject);
        return spDivisionVO;
    }

    @Override
    @TaskAnnotation("getDtoById")
    public SpDivisionDTO getDtoById(SessionFactory factory, String id) {
        SpDivisionVO spDivision = this.getById(factory, id);
        List<SpDivisionProjectVO> spDivisionProjectVOList = spDivisionProjectService.treeByDivisionId(factory, id);
        SpDivisionDTO spDivisionDTO = BeanUtil.toBean(spDivision, SpDivisionDTO.class);
        spDivisionDTO.setLists(spDivisionProjectVOList);
        return spDivisionDTO;
    }

    @Override
    @TaskAnnotation("getByProjectId")
    public SpDivision getByProjectId(SessionFactory factory, String projectId) {
        SpDivisionMapper mapper = factory.getMapper(SpDivisionMapper.class);
        return mapper.selectOneByProjectId(projectId);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpDivisionVO> list(SessionFactory factory, SpDivisionQuery query) {
        SpDivisionMapper mapper = factory.getMapper(SpDivisionMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpDivisionVO> page(SessionFactory factory, PageQuery pageQuery, SpDivisionQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpDivisionVO> spDivisionList = list(factory, query);
        return new PageInfo<>(spDivisionList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpDivision spDivision) {
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spDivision.getProjectId());
        Assert.notNull(spEstablish,"请先进行项目立项！");
        // 默认草稿
        if (StringUtils.isEmpty(spDivision.getStatus())) {
            spDivision.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpDivisionMapper mapper = factory.getMapper(SpDivisionMapper.class);
        return mapper.insertOrUpdate(spDivision) > 0;
    }

    @Override
    @TaskAnnotation("saveOrUpdateDto")
    public boolean saveOrUpdateDto(SessionFactory factory, SpDivisionDTO spDivisionDTO) {
        // 1. 保存主题信息
        this.saveOrUpdate(factory, spDivisionDTO);

        // 2. 保存项目信息
        spDivisionProjectService.removeByDivisionId(factory, spDivisionDTO.getId());
        CommonUtils.sorted(spDivisionDTO.getLists());
        spDivisionDTO.getLists().forEach(e -> {
            e.setDivisionId(spDivisionDTO.getId());
            e.setProjectId(spDivisionDTO.getProjectId());
        });
        spDivisionProjectService.saveTree(factory, spDivisionDTO.getLists());
        return true;
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        // 1. 更新状态
        SpDivision spDivision = new SpDivision();
        spDivision.setId(id);
        spDivision.setStatus(Constants.AppceptStatus.APPROVED);
        spDivision.setFinishTime(new Date());
        return update(factory, spDivision);
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpDivision spDivision) {
        SpDivisionMapper mapper = factory.getMapper(SpDivisionMapper.class);
        return mapper.update(spDivision) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpDivision spDivision = new SpDivision();
        spDivision.setId(id);
        spDivision.setStatus(status);
        return update(factory, spDivision);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpDivision source = getById(factory, id);
        Assert.state(source.getStatus().equals(Constants.AppceptStatus.DRAFT), "非草稿状态不允许删除");
        SpDivisionMapper mapper = factory.getMapper(SpDivisionMapper.class);
        return mapper.hidden(id) > 0;
    }
}
